package com.example.bindserviceatest

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Binder
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        findViewById<TextView>(R.id.tvBindServics).setOnClickListener {
            try {
                val intent = Intent()
                intent.setAction("com.example.servicesa.testservices");
                intent.setComponent(ComponentName("com.example.servicesa","com.example.servicesa.TestServices"))
                var result = bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
                Log.i("TestServices", "bind result:"+result)
            } catch (err:Exception) {
                Log.i("TestServices", "bindService Failed, caused by:"+err.message)
            }

        }
    }

    private val serviceConnection = object:ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            Log.i("TestServices", "onServiceConnected")
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            Log.i("TestServices", "onServiceDisconnected")
        }

    }
}